<?php if (isset($data['tsx'])) { ?>
  <?php
    $i_sl = 0;
    $i_ah = 0;
    $i_pk = 0;
  ?>
  <div id="tieu-de">Nhập bảng lương <?php echo ($data['id_tsx'] ? 'tổ ' : '') . $data['tsx']->ten_to_san_xuat ?> tháng <?php echo date('n/Y', $data['time']) ?></div>
  <form id="nhap-bang-luong-form" action="<?php echo base_path() . 'quy_doanh_nghiep/luong_nhan_vien/nhap_luong/' . $data['tsx']->id ?>" method="post">
    <?php echo $data['nhan_vien'] ?>
    <?php if ($data['id_tsx']) { ?>
      <fieldset style="margin-top: 30px;">
        <legend>Nhập sản lượng tổ</legend>
        <div id="form-item-san-luong-wrapper">
          <?php if ($_POST && isset($_POST['san_luong_id'])) { ?>
          <?php foreach ($_POST['san_luong_id'] as $key => $san_luong_id) { ?>
            <?php $i_sl++ ?>
            <div class="form-item-row">
              <div class="form-item form-text-wrapper">
                <select class="form-select select scl-san-luong-id" name="san_luong_id[]">
                  <?php foreach ($data['gia_luong'] as $gia_luong) { ?>
                    <option value="<?php echo $gia_luong->id ?>" <?php echo ($san_luong_id == $gia_luong->id ? 'selected="selected"' : '')?>><?php echo $gia_luong->name . ' - ' . number_format($gia_luong->don_gia) ?></option>
                  <?php } ?>
                </select>
                <input id="txt-san-luong-<?php echo $i_sl ?>" value="<?php echo $_POST['san_luong'][$key] ?>" name="san_luong[]" type="text" autocomplete="OFF"
                       class="format_currency form-text text txt-san-luong <?php echo (isset($_POST['error']['san_luong']) && $_POST['error']['key'] == $key) ? 'error' : '' ?>" />
              </div>
              <div class="form-item">
                <input type="button" value="X" class="field-remove-submit form-submit"/>
              </div>
              <div class="clear"></div>
            </div>
            <?php } ?>
          <?php } else { ?>
          <?php $i_sl++; ?>
          <div class="form-item-row">
            <div class="form-item form-text-wrapper">
              <select class="form-select select scl-san-luong-id" name="san_luong_id[]">
                <?php foreach ($data['gia_luong'] as $gia_luong) { ?>
                  <option value="<?php echo $gia_luong->id ?>"><?php echo $gia_luong->name . ' - ' . number_format($gia_luong->don_gia) ?></option>
                <?php } ?>
              </select>
              <input id="txt-san-luong-<?php echo $i_sl ?>" value="" name="san_luong[]" type="text" autocomplete="OFF"
                     class="format_currency form-text text txt-san-luong" />
            </div>
            <div class="form-item">
              <input type="button" value="X" class="field-remove-submit form-submit"/>
            </div>
            <div class="clear"></div>
          </div>
          <?php } ?>
        </div>
        <div id="add-more-san-luong-wrapper">
          <input id="add-more-san-luong" type="button" value="Thêm sản lượng" class="field-add-more-san-luong-submit form-submit"/>
        </div>
      </fieldset>
    <fieldset style="margin-top: 30px;">
        <legend>Kiểm tra bảng lương</legend>
        <div id="form-item-thong-kiem-luong-to-wrapper">
          <div class="form-item-row">
            <div class="form-item form-text-wrapper">
              <label>Số công theo tiến độ:</label>
              <label id="lbl-so-cong-tien-do" class="data-text"><?php echo ($data['tsx']->tong_so_cong ? $data['tsx']->tong_so_cong : 0) ?></label>
            </div>
          </div>
          <div class="form-item-row">
            <div class="form-item form-text-wrapper">
              <label>Tổng sản lượng:</label>
              <label id="lbl-tong-san-luong" class="data-text">Chưa có</label>
            </div>
          </div>
          <div class="form-item-row">
            <div class="form-item form-text-wrapper">
              <label>Phạt tổ:</label>
              <label id="lbl-phat-to" class="data-text"><?php echo number_format($data['tsx']->tong_so_phat) ?> <?php if ($data['tsx']->tong_so_phat) { ?>(<a href="javascript:;" onclick="show_phat_to()">Chi tiết</a>)<?php } ?></label>
            </div>
          </div>
          <div class="clear"></div>
          <div class="form-item-row">
            <div class="form-item form-text-wrapper">
              <label>Tổng số công nhập:</label>
              <label id="lbl-so-cong-nhap" class="data-text">Chưa có</label>
            </div>
          </div>
          <div class="form-item-row">
            <div class="form-item form-text-wrapper">
              <label>Tổng số lương:</label>
              <label id="lbl-tong-so-luong" class="data-text">Chưa có</label>
            </div>
          </div>
          <div class="form-item-row">
            <div class="form-item form-text-wrapper">
              <label>Số dư:</label>
              <label id="lbl-so-du" class="data-text">Chưa có</label>
            </div>
          </div>
          <div class="clear"></div>
          <div id="kiem-tra-bang-luong-wrapper">
            <input id="kiem-tra-bang-luong" onclick="return validate_bang_luong();" type="button" value="Kiểm tra bảng lương" class="field-kiem-tra-bang-luong-luong-submit form-submit"/>
          </div>
      </fieldset>
    <?php } ?>
    <div id="submit-wrapper">
      <input type="hidden" id="id-tsx" name="id_tsx" value="<?php echo $data['id_tsx'] ?>" />
      <input type="hidden" id="phat-loi-to" name="phat_loi_to" value="<?php echo ($data['tsx']->tong_so_phat ? $data['tsx']->tong_so_phat : 0) ?>" />
      <input type="hidden" id="tong-so-cong" name="tong_so_cong" value="<?php echo ($data['tsx']->tong_so_cong ? $data['tsx']->tong_so_cong : 0) ?>" />
      <input type="hidden" id="tong-san-luong" name="tong_san_luong" value="" />
      <input type="hidden" id="tong-so-luong" name="tong_so_luong" value="" />
      <input type="hidden" id="so-du" name="so_du" value="" />
      <input type="hidden" id="total-san-luong" value="<?php echo $i_sl  ?>" disabled="disabled"/>
<!--      <input type="hidden" id="total-am-hong" value="--><?php //echo $i_ah  ?><!--" disabled="disabled"/>-->
<!--      <input type="hidden" id="total-phat-khac" value="--><?php //echo $i_pk  ?><!--" disabled="disabled"/>-->
      <?php foreach ($data['gia_luong'] as $gia_luong) { ?>
        <input type="hidden" id="id-san-luong-hidden-<?php echo $gia_luong->id ?>" name="id_san_luong_hidden[<?php echo $gia_luong->id ?>]" class="id-san-luong-hidden" value="<?php echo $gia_luong->id ?>" />
        <input type="hidden" id="name-san-luong-hidden-<?php echo $gia_luong->id ?>" name="name_san_luong_hidden[<?php echo $gia_luong->id ?>]" class="name-san-luong-hidden" value="<?php echo $gia_luong->name ?>" />
        <input type="hidden" id="don-gia-san-luong-hidden-<?php echo $gia_luong->id ?>" name="don_gia_san_luong_hidden[<?php echo $gia_luong->id ?>]" class="don-gia-san-luong-hidden" value="<?php echo $gia_luong->don_gia ?>" />
      <?php } ?>
      <input id="submit" type="submit" onclick="return check_nhap_bang_luong();" value="Nhập bảng lương" class="form-submit"/>
<!--      <input id="submit" type="submit" value="Nhập dữ liệu" class="form-submit" style="display: none;"/>-->
    </div>
  </form>
  <?php $header = array('Ngày tháng', 'Tiền phạt', 'Lý do'); ?>
  <?php if ($data['tsx']->tong_so_phat) { ?>
    <div id="loi-to-dialog" style="display: none;">
      <?php echo theme('table', array('header' => $header, 'rows' => $data['loi_to'])); ?>
    </div>
  <?php } ?>
  <?php foreach ($data['loi_nhan_vien'] as $ma_nv => $loi_nv) { ?>
    <div id="loi-nv-<?php echo $ma_nv ?>-dialog" style="display: none;">
      <?php echo theme('table', array('header' => $header, 'rows' => $loi_nv)); ?>
    </div>
  <?php } ?>
<?php } ?>